package org.example.lanhang.controller;

import org.example.lanhang.VO.UserVO;
import org.example.lanhang.entity.user;
import org.example.lanhang.service.IUserService;
import org.example.lanhang.util.PageResult;
import org.example.lanhang.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gww
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("list")
    public R findall(){
        return R.ok().data("list",userService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize){
        PageResult<user>u=userService.findByPage(page,pageSize);
        return R.ok().data("u",u);
    }
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize, @RequestBody(required = false)UserVO userVO) {
        PageResult<user> u = userService.search(page, pageSize,userVO);
        return R.ok().data("u", u);
    }
    @PostMapping("/add")
    public R add(@RequestBody user user){
        boolean flag=userService.add(user);
        return flag ? R.ok().message("添加学生成功") :R.error().message("添加学生失败");
    }

    @PutMapping("/update")
    public R update(@RequestBody user user){
        boolean flag=userService.updateById(user);
        return flag ? R.ok().message("添加学生成功") :R.error().message("添加学生失败");
    }
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return userService.removeByIds(sids) ? R.ok().message("删除学生成功!") : R.error().message("删除学生失败!");
    }



}
